﻿package com.vinyl.controls
{
	import fl.core.UIComponent;
	import flash.events.*;
	import flash.events.EventDispatcher;
	import flash.display.DisplayObjectContainer;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import com.vinyl.components.EmbedFontSimpleLable;
	
	/**	
	 *	      	
	 */
	[IconFile ("../../../../../@_00_simpleModal/assets/simpleModal.png")]
	public class Alert extends UIComponent
	{	
        public static const DISPLAY_YES_NO:int = 1;
        public static const DISPLAY_OK_CANCEL:int = 2;
		
        private static var confirm:Function;
        private static var cancel:Function;
		private static var close:Function;
		
		private static var type: Number;
		private static var skinType:Number;
		
		public static var NONMODAL:Number = 0x8000;
		public static var YES:Number = 1;
		public static var NO:Number = 2;
		public static var OK:Number = 4;
		public static var CANCEL:Number = 8;
		public static var root: DisplayObject;
		public static var window:Window;
		
		public function Alert()
		{
			super();
		}
		
		override protected function configUI():void
		{
			super.configUI();
		}		
		
		override protected function draw():void
		{
			super.draw();
		}
		
        public static function show( stage:DisplayObjectContainer, 
												title:String, 
												message:EmbedFontSimpleLable, 
												sType:Number, 
												displayType:int = 0, 
												confirmHandler:Function= null, 
												cancelHandler:Function= null, 
												colselHandler:Function= null,
												initHandler:Function=null
												):void
        {
			skinType = sType;
			root = stage.root;
            confirm = confirmHandler;
            cancel = cancelHandler;
			close = colselHandler;
			type = displayType;
			
			var flags;
            switch ( displayType )
            {
				case DISPLAY_YES_NO :
					flags = Alert.YES | Alert.NO;
					break;
				case DISPLAY_OK_CANCEL :
					flags = Alert.OK | Alert.CANCEL;
					break;
				default :
					flags = 0;
					break;
            }
            
			var o = new Object();
			var modal = ( flags & Alert.NONMODAL ) ? false : true;
			
			o.okButton = ( flags &  Alert.OK ) ? true : false;
			o.cancelButton = ( flags & Alert.CANCEL ) ? true : false;
			o.yesButton = ( flags &  Alert.YES ) ? true : false;
			o.noButton = ( flags &  Alert.NO ) ? true : false;
			o.closeButton = true;
			
			if ( o.okButton == false && o.cancelButton == false && o.yesButton == false && o.noButton == false )
			{
				o.okButton = true;
				o.defButton = Alert.OK;
			}
			
			o.title = ""+title;
			o.message = message;
			
			createChild( stage, o, initHandler );
        }
        
		public static function clear()
		{
			if ( Alert.root && Alert.root.stage ) //한번이라도 얼럿이 발생해야 한다
			{
				for( var i = 0; i < Alert.root.stage.numChildren; i++ )
				{
					var mc = Alert.root.stage.getChildAt( i );
					if ( mc && mc.name == "window" ) Alert.root.stage.removeChild( mc );
				}
			}
		}
		
        private static function closeHandler( event:MouseEvent ):void
        {
            switch( type )
            {
				case Alert.YES : case Alert.OK :
					if( confirm != null ) confirm();
				break;
				
				case Alert.NO : case Alert.CANCEL :
					if(cancel != null) cancel();
				break;
            }
			
			if ( close != null ) close();
			
			while( window.skin.numChildren )
				window.skin.removeChildAt( 0 );
			
			Alert.root.stage.removeChild( event.currentTarget.parent );
        }
		
        private static function confirmHandler( event:MouseEvent ):void
        {
			if( confirm != null )confirm();
			Alert.root.stage.removeChild( event.currentTarget.parent );
        }

		private static function cancelHandler( event:MouseEvent ):void
        {
			if( cancel != null )cancel();
			Alert.root.stage.removeChild( event.currentTarget.parent );
        }
		
        private static function createChild( displayObject :DisplayObjectContainer, o: Object, func: Function ):void
        {
			window = new Window();
			window.name = "window";
			
			displayObject.stage.addChild( window );
			root.dispatchEvent( new Event( "alert" ) );
			
			window.skin.addChild( o.message );
			
			window.x = Math.round( root.stage.stageWidth / 2 - 355 / 2 );
			window.y = Math.round( root.stage.stageHeight / 2 - 355 / 2 );
			
			var winWidth = window.width;
			var winHeight = window.height;
			
			window.addEventListener( MouseEvent.MOUSE_DOWN, mouseDownBoxHandler );
			window.addEventListener( MouseEvent.MOUSE_UP, mouseUpBoxHandler );
			window.addEventListener( MouseEvent.MOUSE_OUT, mouseOutBoxHandler );
			
			if( func != null ) 
			{
				if( o.okButton ) 
				{
					var okButton = new OkButton();
					window.addChild( okButton );
					okButton.buttonMode = true;
					okButton.name = "okButton";
					okButton.gotoAndStop( 1 );
					okButton.addEventListener( MouseEvent.CLICK, Alert.confirmHandler );
					
					okButton.x = Math.round( winWidth / 2 - okButton.width / 2 );
					okButton.y = Math.round( winHeight - winHeight / 4 ); 
				}
				
				if( o.cancelButton ) 
				{
					var cancelButton = new CancelButton();
					window.addChild( cancelButton );
					cancelButton.buttonMode = true;
					cancelButton.name = "cancelButton";
					cancelButton.gotoAndStop( 1 );
					cancelButton.addEventListener( MouseEvent.CLICK, Alert.cancelHandler );
					
					okButton.x = Math.round( winWidth / 2 - okButton.width -5 );
					cancelButton.x = Math.round( winWidth / 2 + 5 );
					cancelButton.y = Math.round( winHeight - winHeight / 4 );
				}
				
				if( o.yesButton ) 
				{
					var yesButton = new YesButton();
					window.addChild( yesButton );
					yesButton.buttonMode = true;
					yesButton.name = "yesButton";
					yesButton.gotoAndStop( 1 );
					yesButton.addEventListener( MouseEvent.CLICK, Alert.confirmHandler );
					
					yesButton.x = Math.round( winWidth / 2 - winWidth / 2 );
					yesButton.y = Math.round( winHeight - winHeight / 4 );
				}
				
				if( o.noButton ) 
				{
					var noButton = new NoButton();
					window.addChild( noButton );
					noButton.buttonMode = true;
					noButton.name = "noButton";
					noButton.gotoAndStop( 1 );
					noButton.addEventListener( MouseEvent.CLICK, Alert.cancelHandler );
					
					yesButton.x = Math.round( window.width / 2 - yesButton.width -5 );
					noButton.x = Math.round( window.width / 2 + 5 );
					noButton.y = Math.round( window.height - window.height / 4 );
				}
				
				if( o.closeButton ) 
				{
					var closeButton = new CloseButton();
					window.addChild( closeButton );
					closeButton.x = 294;
					closeButton.y = 12;
					closeButton.buttonMode = true;
					closeButton.name = "closeButton";
					closeButton.gotoAndStop( 1 );
					closeButton.addEventListener( MouseEvent.CLICK, Alert.closeHandler );
				}
				
				func( window, o );
			}
        }
		
		private static function mouseOutBoxHandler(event:MouseEvent):void 
		{
			var sprite:MovieClip = MovieClip( event.target );
			sprite.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveBoxHandler);
			sprite.stopDrag();
		}
		
		private static function mouseDownBoxHandler(event:MouseEvent):void 
		{
			var sprite:MovieClip = MovieClip( event.currentTarget );
			if( sprite.name != "window" ) return;
			
			sprite.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveBoxHandler);
			sprite.startDrag();
		}
		
		private static function mouseUpBoxHandler(event:MouseEvent):void 
		{
			var sprite:MovieClip = MovieClip( event.target );
			sprite.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveBoxHandler);
			sprite.stopDrag();
		}
		
		private static function mouseMoveBoxHandler(event:MouseEvent):void 
		{
			var sprite:MovieClip = MovieClip( event.target );
			
			if( sprite.x + sprite.width > root.stage.stageWidth ) sprite.x = root.stage.stageWidth - sprite.width;
			if( sprite.x < 0 ) sprite.x = 0;
			if( sprite.y + sprite.height > root.stage.stageHeight ) sprite.y = root.stage.stageHeight - sprite.height;
			if( sprite.y < 0 ) sprite.y = 0;
			
			event.updateAfterEvent();
		}
	}
}












